package com.lagou.cl.util;

import com.lagou.cl.pojo.UserInfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * @author 程  林
 * @date 2020-04-18 19:29
 * @description //
 * @since V1.0.0
 */
public class BaseSession {

	private static final String ACCOUNT = "account";

	private HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
	}

	private HttpServletResponse getResponse() {
		return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getResponse();
	}

	public UserInfo getAccount() {
		HttpSession session = getRequest().getSession();
		return (UserInfo) session.getAttribute(ACCOUNT);
	}

	public void setAccount(UserInfo account) {
		HttpSession session = getRequest().getSession();
		if (account != null) {
			session.setAttribute(ACCOUNT, account);
			//session过期时间设置，以秒为单位，即在没有活动30分钟后，session将失效
			session.setMaxInactiveInterval(30 * 60);
		}
	}


}
